//
//  PersonHeaderView.m
//  Cunli
//
//  Created by ZhanyaaLi on 15/12/11.
//  Copyright © 2015年 lilubin. All rights reserved.
//

#import "PersonHeaderView.h"
#import "Config.h"
#import "UIImageView+WebCache.h"
#import "UIView+CYExtenssion.h"
#import "UIColor+expanded.h"
#import "User.h"

#define KCLPORTRAITCHANGE @"portraitChanged"

@implementation PersonHeaderView

/*
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        NSArray  *roleArray = [NSArray arrayWithObjects:@"村民", @"律师", @"", @"调解员", @"联络员", nil];

//        portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 60, 60)];
        portraitView.contentMode = UIViewContentModeScaleAspectFill;
        portraitView.backgroundColor = [UIColor whiteColor];
        [portraitView.layer setCornerRadius:30];
        portraitView.layer.masksToBounds = YES;
        portraitView.translatesAutoresizingMaskIntoConstraints = NO;
        NSString *testImgStr = [Config getPortraitURL];
        [portraitView setImageWithURL:[NSURL URLWithString:testImgStr] placeholderImage:[UIImage imageNamed:@"default_news"]];
        [self addSubview:portraitView];
        
//        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(78, 6, 150, 21)];
        nameLabel.text = [Config getUserTruename];
        nameLabel.font = [UIFont boldSystemFontOfSize:17];
        nameLabel.textColor = [UIColor whiteColor];
        UIColor *bgColor = [UIColor colorWithHexString:@"0x1f1f1f"];
        [nameLabel setBackgroundColor:bgColor];
//        nameLabel.tag = 251;
        nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:nameLabel];
        
//        UILabel *roleLabel = [[UILabel alloc] initWithFrame:CGRectMake(78, 32, 100, 21)];
        roleLabel.text = roleArray[[Config getGroupID].integerValue-5];
        roleLabel.font = [UIFont systemFontOfSize:14];
        roleLabel.textColor = [UIColor colorWithHexString:@"0x9c9c9c"];
        [roleLabel setBackgroundColor:bgColor];
//        roleLabel.tag = 252;
        roleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:roleLabel];
        
        self.backgroundColor = [UIColor colorWithHexString:@"0x1f1f1f"];
        
    }
    return self;
}
 */

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self _addObserve];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self _addObserve];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self _addObserve];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    NSArray  *roleArray = [NSArray arrayWithObjects:@"村民", @"律师", @"", @"调解员", @"联络员", nil];
    
    portraitView.contentMode = UIViewContentModeScaleAspectFill;
    portraitView.backgroundColor = [UIColor whiteColor];
    [portraitView.layer setCornerRadius:30];
    portraitView.layer.masksToBounds = YES;
    portraitView.translatesAutoresizingMaskIntoConstraints = NO;
    NSString *testImgStr = [Config getPortraitURL];
    [portraitView setImageWithURL:[NSURL URLWithString:testImgStr] placeholderImage:[UIImage imageNamed:@"default_news"]];
    
    nameLabel.text = [Config getUserTruename];
    nameLabel.font = [UIFont boldSystemFontOfSize:17];
    nameLabel.textColor = [UIColor whiteColor];
    UIColor *bgColor = [UIColor colorWithHexString:@"0x1f1f1f"];
    [nameLabel setBackgroundColor:bgColor];
    nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    roleLabel.text = roleArray[[Config getGroupID].integerValue-5];
    roleLabel.font = [UIFont systemFontOfSize:14];
    roleLabel.textColor = [UIColor colorWithHexString:@"0x9c9c9c"];
    [roleLabel setBackgroundColor:bgColor];
    roleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.backgroundColor = [UIColor colorWithHexString:@"0x1f1f1f"];
}

- (void)_addObserve
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(portraitChange:) name:KCLPORTRAITCHANGE object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KCLPORTRAITCHANGE object:nil];
}

- (void)portraitChange:(NSNotification *)notification
{
    [self loadImage];
    
}

- (void)loadImage
{
//    NSURL *porURL = [NSURL URLWithString:[Config getPortraitURL]];
    NSURL *porURL = [NSURL URLWithString:[Login curLoginDetailUser].pic];
    NSLog(@"In personHeaderView porURL:%@", [Login curLoginDetailUser].pic);
    [portraitView setImageWithURL:porURL placeholderImage:[UIImage imageNamed:@"default_news"]];
}


@end
